import Utils from "@/$ma/common/utils/utils.js"

// 数字换算
let numberFilter = item => {
	if (item >= 10000) {
		return Math.round(item/1000)/10 + 'w';
	} else if (item >= 1000) {
		return Math.round(item/100)/10 + 'k';
	} else {
		return item;
	}
}

let numberFilter2 = item => {
	if(typeof(item) == 'number'){
		return item.toString().replace(/\d+/, function(n) {
		   return n.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
	    });
	}else return '0';
}

// 文字截断
let textFilter = item => {
	if(!item) return '';
	let length = 40;
	if(item.length > 40) return item.substring(0, length) + '...';
	else return item;
}

// 时间戳格式化
let timeFilter = time => {
	if (time != null) {
		if(typeof(date) == 'string') time = date.replace(/-/g,'/');   // 解决iOS端，时间显示NaN的原因
		var date = new Date(time);
		let year = date.getFullYear().toString().padStart(2,0);
		let mon = (date.getMonth() + 1).toString().padStart(2,0);
		let datee = date.getDate().toString().padStart(2,0);
		let hours = date.getHours().toString().padStart(2,0);
		let minu = date.getMinutes().toString().padStart(2,0);
		// console.log(year,mon,datee,hours,minu)
		let formatting = year + '-' + mon + '-' + datee + ' ' + hours + ":" + minu;
		
		if (Utils.onTime(date)) {
			return Utils.onTime(date);
		} else {
			return formatting;
		}
	 }
}

//价钱显示
let moneyFilter = item => {
	if(item){
		let _item = `${item}`;
		if(_item.indexOf('.') > 0){
			return `${_item}`;
		}else{
			return `${_item}.00`;
		}
	}else {
		return '0.00';
	}
}


export default {
	numberFilter,
	textFilter,
	timeFilter,
	moneyFilter,
	numberFilter2
}